#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Copyright © 2014, Finium Sdn Bhd, All Rights Reserved
 * 
 * RedisRepository.java
 * Modification History
 * *************************************************************
 * Date				Author						Comment
 * Sep 25, 2012		Venkaiah Chowdary Koneru	Created
 * *************************************************************
 */
package ${package}.redis.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

/**
 * repository for redis which contains all the CRUD related tasks
 * 
 * @author Venkaiah Chowdary Koneru
 */
@Repository
public class RedisRepository
{
    private static final Log LOG = LogFactory.getLog(RedisRepository.class);

    @Autowired
    private RedisTemplate<String, byte[]> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * retrieve value from redis
     * 
     * @param key
     * @return value
     */
    public byte[] getValue(final String key)
    {
        LOG.debug("Retreiving values for key[" + key + "]...");
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * retrieve strign value from redis
     * 
     * @param key
     * @return string value
     */
    public String getStrValue(final String key)
    {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * set string value
     * 
     * @param key
     * @param value
     */
    public void setStrValue(final String key, String value)
    {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * saves string key/value pair into redis
     * 
     * @param key
     * @param value
     */
    public void setValue(final String key, final byte[] value)
    {
        LOG.info("Inserting string value into redis with key [" + key + "].");
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * saves key/value pair of list
     * 
     * @param key
     * @param value
     */
    public void setList(final String key, final byte[] value)
    {
        LOG.info("Inserting list value into redis with key [" + key + "]..");
        redisTemplate.opsForList().rightPush(key, value);
    }

    /**
     * saves key/value pair for object.
     * 
     * @param key
     * @param value
     * @param onlyIfAbsent
     */
    public boolean setValue(final String key, final byte[] value,
            boolean onlyIfAbsent)
    {
        LOG.info("Inserting object value into redis with key [" + key + "]....");
        if (onlyIfAbsent)
        {
            return redisTemplate.opsForValue().setIfAbsent(key, value);
        }

        setValue(key, value);
        return true;
    }

    /**
     * retrieves all the list of objects for a given key
     * 
     * @param key
     * @return list of objects
     */
    public List<byte[]> getList(final String key)
    {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * Checks whether the code exists in redis store or not.
     * 
     * @param key
     *            key to check on redis store
     * @return true or false
     */
    public boolean hasKey(final String key)
    {
        LOG.debug("Searching for key[" + key + "] exists");
        return redisTemplate.hasKey(key);
    }

}
